package com.leecreate.tutool.controller;

import com.leecreate.tutool.domain.AjaxResult;
import com.leecreate.tutool.domain.HardPullExt;
import com.leecreate.tutool.domain.hardpull.HardPullDto;
import com.leecreate.tutool.domain.hardpull.HardPullVo;
import com.leecreate.tutool.service.HardPullService;
import com.leecreate.tutool.utils.DateUtils;
import com.leecreate.tutool.utils.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Optional;

@RestController
@RequestMapping("/hardPull")
public class HardPullController {

    @Value("${tu.whitelist}")
    private String whitelist;
    @Resource
    private HardPullService hardPullService;
    @Autowired
    private MongoTemplate mongoTemplate;

    @PostMapping("/query")
    @ResponseBody
    public AjaxResult query(HttpServletRequest request, @RequestBody HardPullVo hardPullVo) {
        String ipAddress = IpUtils.getIpAddr(request);
        HardPullExt hardPullExt = new HardPullExt();
        hardPullExt.setQueryDate(DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        hardPullExt.setHardPullVo(hardPullVo);
        hardPullExt.setIdNumber(hardPullVo.getIdNumber());
        hardPullExt.setIpAddress(ipAddress);
        if (whitelist.contains(ipAddress)) {
            Query query = new Query();
            Criteria criteria = new Criteria();
            criteria.and("idNumber").is(hardPullVo.getIdNumber());
            criteria.and("success").is(true);
            query.addCriteria(criteria);
            Optional<HardPullExt> optional = mongoTemplate.find(query, HardPullExt.class, "sys_hard_pull_query").stream().findFirst();
            if (optional.isPresent()) {
                if (hardPullVo.isForceRefresh()) {
                    HardPullDto hardPullDto = hardPullService.bureauHardPull(hardPullVo);
                    hardPullExt.setHardPullDto(hardPullDto);
                    hardPullExt.setHistory(false);
                    hardPullExt.setSuccess(hardPullDto.isSuccess());
                } else {
                    hardPullExt.setHardPullDto(optional.get().getHardPullDto());
                    hardPullExt.setHistory(true);
                }
            } else {
                HardPullDto hardPullDto = hardPullService.bureauHardPull(hardPullVo);
                hardPullExt.setHardPullDto(hardPullDto);
                hardPullExt.setHistory(false);
                hardPullExt.setSuccess(hardPullDto.isSuccess());
            }
        } else {
            hardPullExt.setSuccess(false);
        }
        mongoTemplate.insert(hardPullExt, "sys_hard_pull_query");
        if (hardPullExt.getHardPullDto() != null) {
            return AjaxResult.success("", hardPullExt.getHardPullDto());
        } else {
            return AjaxResult.error("IP受限，请联系管理员");
        }

    }
}
